//
//  WBHomeViewController.swift
//  WeiBoApp
//
//  Created by CrazyHacker on 16/6/29.
//  Copyright © 2016年 CrazyHacker. All rights reserved.
//

import UIKit

private let cellId = "cellId"
class WBHomeViewController: WBBaseViewController {
    lazy var newsList = [String]()
    /// 视图模型对象
    private lazy var viewModel = WBStatusViewModel()
    
    @objc private func pushNextVc() {
        let vc = WBDemoViewController()

        navigationController?.pushViewController(vc, animated: true)
    }


}
// MARK: - 加载微博数据
extension WBHomeViewController {
    override func loadData() {
     
        viewModel.loadStatusList(pullup: self.isPullUp) { (isSuccess,isRefresh) in
            self.refreshControl.endRefreshing()
            self.isPullUp = false
            
            
            self.tableView?.reloadData()
        }
    }
}

extension WBHomeViewController {
    override func setupTableView() {
        super.setupTableView()
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(pushNextVc))

        // 注册原型cell
         tableView?.register(UINib(nibName: "WBHomeStatusCell", bundle: nil), forCellReuseIdentifier: cellId)
        setupNavigationTitle()
    }
}

extension WBHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // 查询原型cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.textLabel?.text = viewModel.statusList[indexPath.row].text
        return cell
    }
    
}

extension WBHomeViewController {
    private func setupNavigationTitle() {
        let title = WebNetworkTools.shared.userAccount.screen_name
        let titleButton = WBHomeTitleButton(title: title)
//        
//        let btn = UIButton.cz_textButton("画江湖", fontSize: 17, normalColor: UIColor.darkGray(), highlightedColor: UIColor.black())
//        btn?.setImage(UIImage(named: "navigationbar_arrow_down"), for: [])
//        btn?.setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        navItem.titleView = titleButton
        
        titleButton.addTarget(self, action: #selector(clickTitleView), for: .touchUpInside)
    }
    
    @objc private func clickTitleView(btn: UIButton) {
        // 点击更改相反状态
        btn.isSelected = !btn.isSelected
    }
}

